<?php

namespace app\adminapi\model;

use app\admin\model\school\SearchCity;
use app\common\model\dyqc\ManystoreShop;
use think\Model;
use think\Session;

class Admin extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $hidden = [
        'password',
        'salt'
    ];




    /** 通用详情(后台api版本)
     * @param $params
     * @param $trans
     * @return $this
     * @throws \Exception
     */
    public function detail($id,$show_field=[],$except_field=[]){
        $row = $this->get($id);
        if (!$row) {
            throw new \Exception(__('No Results were found'));
        }
        if($show_field){
            $row->visible($show_field);
        }
        if($except_field){
            $row->hidden($except_field);
        }
        return $row;
    }


    /**
     * 获取会员的组别
     */
    public function getGroupsAttr($value, $data)
    {
        $group_ids = \app\admin\model\api\AuthGroupAccess::where("uid", $data['id'])->column("group_id");
        if(!$group_ids)return [];

        $groups = \app\admin\model\api\AuthGroup::where("id","in",$group_ids)->select();
        return $groups;
    }






    public static function init()
    {
        self::beforeWrite(function ($row) {
            $changed = $row->getChangedData();
            //如果修改了用户或或密码则需要重新登录
            if (isset($changed['username']) || isset($changed['password']) || isset($changed['salt'])) {
                $row->token = '';
            }
        });
    }

//    public function getAddressCityList(){
//        return SearchCity::column("address_city","id");
//    }


    public static function getHaveShopId($adminId){
        $admin = self::get($adminId);
        if(!$admin)throw new \Exception("管理员不存在");
        $area_json = $admin->area_json;
        $shop_id = "*";
        if($area_json){
           $shop_id = [];
           $searchCity = SearchCity::where("id","in",$area_json)->select();
           foreach ($searchCity as $item){
               $province = $item->province;
               $city = $item->city;
               $district = $item->district;
               //查询在该区域的店铺id
              $manystoreShop =  new ManystoreShop;
              if($province)$manystoreShop = $manystoreShop->where("province",$province);
              if($city)$manystoreShop = $manystoreShop->where("city",$city);
              if($district)$manystoreShop = $manystoreShop->where("district",$district);
               $shop_id = array_merge($shop_id,$manystoreShop->column("id"));
           }
        }
        return $shop_id;
    }

    public static function getHaveCity($adminId){
        $provinces = $citys = $districts = $address_citys = [];
        $admin = self::get($adminId);
        if(!$admin)return compact("provinces","citys","districts","address_citys");
        $area_json = $admin->area_json;

        if($area_json){
            $searchCity = SearchCity::where("id","in",$area_json)->select();
            foreach ($searchCity as $item){
                $address_citys[] = $item->address_city;
                $province = $item->province;
                if($province)$provinces[] = $province;
                $city = $item->city;
                if($city)$citys[] = $city;
                $district = $item->district;
                if($district)$districts[] = $district;
            }
        }
        if(!$provinces)$provinces="*";
        if(!$citys)$citys="*";
        if(!$districts)$districts="*";
        if(!$address_citys)$address_citys="*";
        return compact("provinces","citys","districts","address_citys");
    }



    public static function checkAuthMsg($adminId){
        ["provinces" =>$provinces,"citys"=> $citys, "districts"=>$districts,"address_citys"=>$address_citys] = self::getHaveCity($adminId);
        if(is_array($address_citys))$address_citys = implode(",",$address_citys);
            //必要信息已完善
            return '<div class="alert alert-success-light">
           <b >您当前的区域管理权限为:<span style="color: red"> '.$address_citys.' </span>(能管理该区域下的机构信息，如果是*则不限制)</b></div>';

    }


}
